const app = getApp()
Page({
  data: {
    imgUrl: app.globalData.imgUrl,
    localImg: app.globalData.staticimgUrl,  // 本地图片
    currentTab: 0,
    msgList: [],
    orderType: 1, // 1.待服务 2.已完成
    pagenum: 1,
    pagesize: 5,
    hasMore: true,
    orderinfo: [],
    info: "",
    doctorinfo: null,
    pageName: 'index', // 控制tabbar图标
    falseed: false,
    trueed: false,
    myCustomerNum:""
  },

  onLoad: function(options) {
    // app.showLoading();
    let isLogin = wx.getStorageSync('isLogin');
    if(isLogin == 1) {
      this.loadPageData().then(function () {
        // wx.hideLoading();
      });
    } 
  },

  loadPageData: function() {
    return Promise.all([
      this.getDoctorDetail(),
      this.getNews(),
      this.getCustomer(),
      this.loadOrder()
    ]);
  },
  // 手术完成
  accomplish: function(e) {
    var that = this;
    var orderid = e.currentTarget.dataset.orderid;
    app.request({
      url: 'shopp/yimeiGoodsOrderApi/serviceComplete',
      data: {
        orderid: orderid
      },
      success: function(res) {
        wx.showToast({
          title: res.message,
          icon: 'success',
          duration: 1500,
          success: function(){
            wx.startPullDownRefresh();
          }
        });
      }
    });
  },

  getNews: function() {
    var that = this;
    var doctorId = wx.getStorageSync('userid');
    // 行业新闻
    app.request({
      url: 'system/newsapi/getnewsbystate',
      data: {
        pageSize: 10, //每页数量
        pageNum: 1,  //页数
        type: 0
      },
      success: function(res) {
        that.setData({
          msgList: res.data
        })
      },
      fail: function (res) {
        // app.requestFail(res);
      }
    });
  },

  getDoctorDetail: function() {
    var that = this;
    var doctorId = wx.getStorageSync('userid');
    app.request({
      url: 'doctor/doctor/getDoctorDetail',
      data: {
        'doctorId': doctorId // 修改医生id
      },
      success(res) {
        wx.hideLoading();
        that.setData({
          'doctorinfo': res.data
        })
      },
      fail: function (res) {
        // app.requestFail(res);
      }
    });
    
  },
  // 我的客户
  getCustomer(){
    const that =this;
    const doctorId = wx.getStorageSync('userid');
    app.request({
      url: "shopp/orderApi/getMyCustomerNum?loginUserId="+doctorId,
      method: "GET",
      success: function (res) {
        that.setData({
          myCustomerNum: res.data.myCustomerNum
        })
      },
      fail: function (res) { }
    });
  },
  //滑动切换
  swiperTab: function(e) {
    var that = this;
    that.setData({
      pagenum: 1,
      hasMore: true,
      currentTab: e.detail.current
    });
  },
  //点击切换
  clickTab: function(e) {
    var that = this;
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      let currentTab = e.target.dataset.current;
      that.setData({
        pagenum: 1,
        hasMore: true,
        currentTab: currentTab,
        orderType: Number(currentTab)+1
      });
      that.loadOrder();
    }
  },

  // 加载医生端首页订单信息
  loadOrder: function(e) {
    var that = this;
    let { orderType, pagenum, pagesize, hasMore, orderinfo } = this.data;
    app.request({
      url: 'doctor/doctor/getOrderByDoctor',
      data: {
        state: orderType,
        pageNo: pagenum, // 页码
        pageSize: pagesize, //行数
      },
      success(res) {
        let list;
        if (pagenum == 1) {
          list = res.data;
        }
        else{
          list = orderinfo.concat(res.data);
        } 
        if (res.data.length<pagesize){
          that.setData({
            hasMore: false
          });
        }
        that.setData({
          'orderinfo': list
        });
      }
    })
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.setData({
      pagenum: 1,
      hasMore: true,
      currentTab: 0,
      orderType: 1,
    });
    this.loadPageData().then(function () {
      wx.stopPullDownRefresh();
    });
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom(){
    let { hasMore } = this.data;
    let pagenum = this.data.pagenum+1;
    console.log(pagenum);
    if(hasMore) {
      this.setData({
        pagenum: pagenum
      });
      this.loadOrder();
    }
    else{
      wx.showToast({
        title: '没有更多了哦~',
        icon:'none',
        duration:1000
      });
    }
  },
  // 进入我的客户
  entryMyCustomer(){
    wx.navigateTo({
      url: '/pages/doctor-my-customer/doctor-my-customer'
    });
  },
  // 订单详情
  enrtyDetail(e) {
    console.log(e.currentTarget.dataset.orderid)
    const orderid = e.currentTarget.dataset.orderid;
    wx.navigateTo({
      url: '/pages/doctor-customer-order-detail/doctor-customer-order-detail?orderid=' + orderid
    });
  },
  onShareAppMessage: function() {
    return {
      title: '分享好友获取美丽币！',
      path: '/pages/index/index',
      success: function() {
        app.request({
          url: 'doctor/memberTaskApi/addBeauty',
          data: {
            userId: wx.getStorageSync('userid'),
            type: 3
          },
          success(res) {
            console.log('分享给美丽币给美丽值', res);
          }
        })
      }
    }
  }
})